package com.lj.audiorecorderapp.slice;

import com.lj.audiorecorderapp.ResourceTable;
import cafe.adriel.androidaudiorecorder.OhosAudioRecorder;
import cafe.adriel.androidaudiorecorder.AudioRecorderAbility;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.Window;

/**
 * ex.
 * MainAbilitySlice
 *
 * @author lj
 * @since 2021-05-13
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * 常量int值 constantVal
     */
    public static final int CONSTANTVAL = 200;
    /**
     * 音频文件路径 AUDIO_FILE_PATH
     */
    private String audioFilePath = getFilesDir() + "/recorded_audio.aac";

    @Override
    public void onStart(Intent intent) {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK.getValue());
        window.setStatusBarVisibility(Component.VISIBLE);

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button btnAudio = (Button) findComponentById(ResourceTable.Id_btn_record);
        btnAudio.setClickedListener(component -> startAbilitys());
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        if (requestCode == OhosAudioRecorder.REQUESTCODE) {
            if (resultCode == OhosAudioRecorder.RESULT_OK) {
                new ToastDialog(getContext())
                        .setText("Audio recorded successfully!")
                        // Toast显示在界面中间
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0,CONSTANTVAL)
                        .show();
            } else if (resultCode == OhosAudioRecorder.RESULT_CANCELED) {
                new ToastDialog(getContext())
                        .setText("Audio was not recorded!")
                        // Toast显示在界面中间
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0,CONSTANTVAL)
                        .show();
            }
        }
    }

    /**
     * 启动某一个界面
     */
    private void startAbilitys() {
        Intent intent = new Intent();
        intent.setParam(OhosAudioRecorder.EXTRA_FILE_PATH, audioFilePath);
        intent.setParam(OhosAudioRecorder.EXTRA_COLOR, Color.getIntColor("#F4511E"));
        intent.setParam(OhosAudioRecorder.EXTRA_SOURCE, AudioSource.MIC);
        intent.setParam(OhosAudioRecorder.EXTRA_CHANNEL, AudioChannel.MONO);
        intent.setParam(OhosAudioRecorder.EXTRA_SAMPLE_RATE, AudioSampleRate.HZ_48000);
        intent.setParam(OhosAudioRecorder.EXTRA_AUTO_START, false);
        intent.setParam(OhosAudioRecorder.EXTRA_KEEP_DISPLAY_ON, true);
        /*
         * 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
         */
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getAbilityPackageContext().getBundleName())
                .withAbilityName(AudioRecorderAbility.class.getName())
                .build();
        intent.setOperation(operation);
        /*
         * 把operation设置到intent中
         */
        startAbilityForResult(intent, OhosAudioRecorder.REQUESTCODE);
    }
}
